package com.cloud.ratelimiter;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.validation.Validator;

import java.util.List;

/**
 * @Author: TongRui乀
 * @Date: 2020-02-25 13:53
 * @description：
 */
@Configuration
public class GatewayRedisRateLimiterConfig {

    @Bean
    @Primary
    public GatewayRedisRateLimiter gatewayRedisRateLimiter(ReactiveRedisTemplate<String, String> redisTemplate,
                                                           @Qualifier(GatewayRedisRateLimiter.REDIS_SCRIPT_NAME) RedisScript<List<Long>> redisScript,
                                                           @Qualifier("webFluxValidator") Validator validator) {

        return new GatewayRedisRateLimiter(redisTemplate, redisScript, validator);
    }


    @Bean
    public RateLimiterGatewayFilterFactory rateLimiterGatewayFilterFactory(GatewayRedisRateLimiter gatewayRedisRateLimiter) {

        CustomerKeyResolver customerKeyResolver = new CustomerKeyResolver();

        return new RateLimiterGatewayFilterFactory(gatewayRedisRateLimiter, customerKeyResolver);
    }

}
